import 'package:flutter/cupertino.dart';
import '../models/user_storm_info.model.dart';
import '../store/user.dart';
import '../views/layout.dart';
import '../views/login.dart';
import '../views/refine_user_info.dart';
import 'index.dart';

class AppRouter {
  static PageRouteBuilder generateRoute(RouteSettings settings) {
    final String? name = settings.name;
    // final Map<dynamic, dynamic> userInfo = UserStore.getUserInfo();
    final UserInfoModel userInfo = UserStore.getUserInfo();
    // final bool isLoggedIn = userInfo.containsKey('token') &&
    //     userInfo['token'] != null &&
    //     userInfo['token'] != '';
    final bool isLoggedIn = userInfo.token.isNotEmpty;
    var builder = routeList[name];
    builder ??= routeList['/notFound'];
    // return  _buildPageRoute(RefineUserInfo());
    if (!isLoggedIn && name != '/login' && name != '/loginTo') return _buildPageRoute(LoginPage());
    if(isLoggedIn && (name=='/login' || name == '/loginTo')) return _buildPageRoute(LayoutPage());
    return _buildPageRouteWithArguments(builder!, settings);
  }

  static PageRouteBuilder _buildPageRouteWithArguments(
          WidgetBuilder builder, RouteSettings settings) =>
      PageRouteBuilder(
        pageBuilder: (context, animation, secondaryAnimation) =>
            builder(context),
        transitionsBuilder: (context, animation, secondaryAnimation, child) {
          const begin = Offset(1.0, 0.0);
          const end = Offset.zero;
          const curve = Curves.easeInOut;
          late Animatable<Offset> tween =
              Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
          late Animation<Offset> offsetAnimation = animation.drive(tween);
          return SlideTransition(position: offsetAnimation, child: child);
        },
        transitionDuration: Duration(milliseconds: 300),
        reverseTransitionDuration: Duration(milliseconds: 300),
      );

  static PageRouteBuilder _buildPageRoute(Widget page) => PageRouteBuilder(
        pageBuilder: (context, animation, secondaryAnimation) => page,
        transitionsBuilder: (context, animation, secondaryAnimation, child) {
          const begin = Offset(1.0, 0.0);
          const end = Offset.zero;
          const curve = Curves.easeInOut;
          late Animatable<Offset> tween =
              Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
          late Animation<Offset> offsetAnimation = animation.drive(tween);
          return SlideTransition(position: offsetAnimation, child: child);
        },
        transitionDuration: Duration(milliseconds: 300),
        reverseTransitionDuration: Duration(milliseconds: 300),
      );
}
